package redis

import (
	"bawei_2112a_shop/appconfig"
	"fmt"
	"github.com/go-redis/redis"
	"sync"
)

var (
	RedisClient *redis.Client
	err         error
	once        sync.Once
)

func InitRedis() {
	once.Do(func() {
		host := appconfig.AppConf.RedisConfig.Host
		port := appconfig.AppConf.RedisConfig.Port
		password := appconfig.AppConf.RedisConfig.Password
		DbName := appconfig.AppConf.RedisConfig.DbName
		Addr := fmt.Sprintf("%s:%d", host, port)

		RedisClient = redis.NewClient(&redis.Options{
			Addr:     Addr,
			Password: password, // no password set
			DB:       DbName,   // use default DB
		})

		pong, err := RedisClient.Ping().Result()
		if err != nil {
			fmt.Println("redis 连接失败：")
			fmt.Println(pong, err)
			return
		}

		fmt.Println("redis 连接成功")
	})

}
